package com.ibook.service.impl;

import com.ibook.common.Const;
import com.ibook.common.ServerResponse;
import com.ibook.dao.UserMapper;
import com.ibook.pojo.User;
import com.ibook.service.UserService;
import com.ibook.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by heziqi on 2017/6/9.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    public ServerResponse<User> login(String userIden, String password){
        if(StringUtils.isBlank(userIden) || StringUtils.isBlank(password)){
            return ServerResponse.createByErrorMessage("用户名或密码不能为空！");
        }
        Integer userId = userMapper.selectByUserIden(userIden);
        if(userId == null){
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        String md5Password = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectByUserIdAndPassword(userId,md5Password);
        if (user == null){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        userMapper.updateStatus(!user.getUserStatus(),user.getUserId());
        user.setUserPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登陆成功",user);
    }

    public ServerResponse logout(User user){
        userMapper.updateStatus(!user.getUserStatus(),user.getUserId());
        return ServerResponse.createBySuccessMessage("退出登录成功");
    }

    public ServerResponse register(User user){
        ServerResponse validResponse = this.checkValid(user.getUserName(),Const.USERNAME);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
        validResponse = this.checkValid(user.getUserEmail(),Const.EMAIL);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
        validResponse = this.checkValid(user.getUserPhone(),Const.PHONE);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
        //MD5加密
        user.setUserPassword(MD5Util.MD5EncodeUtf8(user.getUserPassword()));
        int resultCount = userMapper.insertSelective(user);
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createBySuccessMessage("注册成功");
    }

    public ServerResponse checkValid(String str,String type){
        if(StringUtils.isNotBlank(type)){
            //开始校验
            if(Const.USERNAME.equals(type)){
                Integer resultCount = userMapper.selectByUserIden(str);
                if(resultCount != null ){
                    return ServerResponse.createByErrorMessage("用户名已存在");
                }
            }
            if(Const.EMAIL.equals(type)){
                Integer resultCount = userMapper.selectByUserIden(str);
                if(resultCount != null ){
                    return ServerResponse.createByErrorMessage("email已存在");
                }
            }
            if(Const.PHONE.equals(type)){
                Integer resultCount = userMapper.selectByUserIden(str);
                if(resultCount != null ){
                    return ServerResponse.createByErrorMessage("手机号已存在");
                }
            }
        }else{
            return ServerResponse.createByErrorMessage("参数错误");
        }
        return ServerResponse.createBySuccessMessage("校验成功");
    }
}
